$('#escape').on('click', selectPoints);
$('#pathplanning').on('click', findPath);

var escapePoints = new Array();
var resultRoute = new Array(); 
var components = new Array();         
var routeLength;
var pointTimeId; 
var path_fly, path_fly_2, path_fly_3;
function selectPoints(){
//交互提醒
    common.showMsg('人员所在位置',2);

    $('#firestart').addClass('hide');
    $('#escape').addClass('hide');
    $('#startfly').addClass('hide');
    $('#floorclose').addClass('hide');
    $('#stopfly').addClass('hide');

    var layer_upper = viewer.scene.layers.find("F1@听海大道地下空间20201016");
    layer_upper.visible = false;
    viewer.scene.camera.setView({
        destination:{ x: -2386275.095372156, y: 5386727.290105708, z: 2445134.9587714258 },
        orientation:{heading:1.5549424995174963, pitch:-1.5350281966828634, roll:0}
    })

    viewer.entities.removeAll()
    clearInterval(pointTimeId)
    escapePoints = [];
    var flag = 0;
    var handler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);
    handler.setInputAction(function(e){
        var position = viewer.scene.pickPosition(e.position);
        var cartographic = Cesium.Cartographic.fromCartesian(position);
        var longitude = Cesium.Math.toDegrees(cartographic.longitude);
        var latitude = Cesium.Math.toDegrees(cartographic.latitude);
        var height = cartographic.height;

        escapePoints.push({"x":Number(longitude), "y":Number(latitude)})
        if(flag==0){
            viewer.entities.add(new Cesium.Entity({
                point: new Cesium.PointGraphics({
                    color: new Cesium.Color(1,0,0), //red
                    pixelSize: 13,
                    outlineColor : new Cesium.Color(0, 1, 1) //cyan
                }),
                position : Cesium.Cartesian3.fromDegrees(longitude, latitude , 62)
            }));
            
//交互提醒
if(viewer.scene.primitives._primitives[1]){
    //火情模拟
    common.showMsg('自定义逃离点',2);
} else{
    //室内导航
    common.showMsg('加入路径点', 2);
    //清除前序barrier area
    barrierArea = [];
}
            
        }
        if(flag==1){
            viewer.entities.add(new Cesium.Entity({
                point: new Cesium.PointGraphics({
                    color: new Cesium.Color(1,1,0), //yellow
                    pixelSize: 13,
                    outlineColor : new Cesium.Color(0, 1, 1) //cyan
                }),
                position : Cesium.Cartesian3.fromDegrees(longitude, latitude , 62)
            }));
            // handler = handler && handler.destroy();
            handler.destroy();
            //打开展示路径按钮
            $('#pathplanning').removeClass('hide');
        }
        flag++;
    }, Cesium.ScreenSpaceEventType.LEFT_CLICK);
}

function findPath(){
    //开闭界面选项
    $('#pathplanning').addClass('hide');
    $('#floorclose').removeClass('hide');
    $('#stopfly').removeClass('hide');
    var findPathService, parameter, analystParameter, resultSetting;

    resultSetting = new SuperMap.REST.TransportationAnalystResultSetting({
        returnEdgeFeatures: true,
        returnEdgeGeometry: true,
        returnEdgeIDs: true,
        returnNodeFeatures: true,
        returnNodeGeometry: true,
        returnNodeIDs: true,
        returnPathGuides: true,
        returnRoutes: true
    });
    
    analystParameter = new SuperMap.REST.TransportationAnalystParameter({
        barrierNodeIDs:barrierArea,
        resultSetting: resultSetting,
        weightFieldName: "SmLength"
    });
    parameter = new SuperMap.REST.FindPathParameters({
        isAnalyzeById: false,
        nodes: escapePoints,
        hasLeastEdgeCount: false,
        parameter: analystParameter
    });
    findPathService = new SuperMap.REST.FindPathService(SYS.analysisUrl,{
        eventListeners: {
            "processCompleted": processCompleted,
            "processFailed": processError
        }
    });
    findPathService.processAsync(parameter);

    function processCompleted(findPathEventArgs) {
        components = [];
        routeLength = null;
        resultRoute = [];
        var result = findPathEventArgs.result;
        components = result.pathList[0].route.components[0].components;
        routeLength = result.pathList[0].route.components[0].components.length;
        for(k=0; k<routeLength; k=k+1){
            resultRoute.push(result.pathList[0].route.components[0].components[k].x)
            resultRoute.push(result.pathList[0].route.components[0].components[k].y)
            resultRoute.push(62)
        };     
        drawLine(resultRoute);
        playLine();
    };
    function processError(findPathEventArgs){
        console.log('findPathFail');
        //2.2  修改----------------------
        common.showMsg('你无处可逃……', 1);
        //-------------------------------
    }
    function playLine(){
        var direction = null;
        var redirection = null;
        if(escapePoints[1].y>escapePoints[0].y){
            direction = +Math.PI*1/10;
            redirection = Math.PI*9/10;
            distance = -0.0015;
        }
        if(escapePoints[1].y<escapePoints[0].y){
            direction = Math.PI*9/10;
            redirection = +Math.PI*1/10;
            distance = 0.0015;
        }
        path_fly = setTimeout(function(){
            //恢复上盖
            var layer_upper = viewer.scene.layers.find("F1@听海大道地下空间20201016");
            layer_upper.visible = true;

            viewer.camera.setView({
                destination:Cesium.Cartesian3.fromDegrees(
                    components[0].x - 0.0003,
                    components[0].y + distance,
                    94
                    ),
            orientation:{
                heading:direction + Math.atan(
                    (components[routeLength-1].x-components[0].x)/(components[routeLength-1].y-components[0].y)
                    ),
                pitch:-0.2,
                roll:0  
            },
            // duration:2
            });
            var i = 0;
            pointTimeId = setInterval(function(){

                if(i > 0){
                    viewer.entities.removeById(50000 + i)
                }
                if (i == routeLength){
                    clearInterval(pointTimeId);
                    
                }
                if (i < routeLength){
                    viewer.entities.add(new Cesium.Entity({
                        point: new Cesium.PointGraphics({
                            color: new Cesium.Color(0,1,1), //cyan
                            pixelSize: 10,
                            outlineColor : new Cesium.Color(0, 1, 1) //cyan
                        }),
                        id: 50001 + i,
                        position : Cesium.Cartesian3.fromDegrees(components[i].x, components[i].y, 62)
                    }));
                }
                i++;
            }, 20)
        }, 2000);
        path_fly_2 = setTimeout(function(){
            viewer.camera.flyTo({
                destination:Cesium.Cartesian3.fromDegrees(
                    components[routeLength-1].x -0.0003,
                    components[routeLength-1].y - distance,
                    94
                    ),
            orientation:{
                heading: redirection + Math.atan(
                    (components[routeLength-1].x-components[0].x)/(components[routeLength-1].y-components[0].y)
                    ),
                pitch:-0.2,
                roll:0  
            },
            duration:10
            })
        },5000)

        path_fly_3 = setTimeout(function(){
            $('#routefly').removeClass('hide');
            $('#floorclose').removeClass('hide');
        },15000)

    }
    function drawLine(resultRoute){
        viewer.entities.add(new Cesium.Entity({
            polyline: new Cesium.PolylineGraphics({
                material: Cesium.Color.CHARTREUSE,
                width: 3,
                positions: Cesium.Cartesian3.fromDegreesArrayHeights(resultRoute),
            }),
        }))
    }
}

